/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbquestion_client.form;

import bllobjects.Account;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.JFrame;

/**
 *
 * @author salm
 */
public class ClientForms
{
    public static final int CHATFORM_PRIVATE = 2;
    public static final int CHATFORM_ROOM = 1;
    public static final int CHATFORM_WORLD = 0;
    
    private friendRequest _friendRequest;
    private loginForm _loginForm;
    private menu _menu;
    private userInfo _userInfo;
    private Map<String, JFrame> chatFormShown = new HashMap();

    /**
     * @return the _friendRequest
     */
    public friendRequest getFriendRequest() {
        if (_friendRequest == null)
        {
            _friendRequest = new friendRequest();
        }
        return _friendRequest;
    }

    /**
     * @return the _loginForm
     */
    public loginForm getLoginForm() {
        if (_loginForm == null)
        {
            _loginForm = new loginForm();
        }
        return _loginForm;
    }

    /**
     * @return the _menu
     */
    public menu getMenu() {
        if (_menu == null)
        {
            _menu = new menu();
        }
        return _menu;
    }

    /**
     * @return the _userInfo
     */
    public userInfo getUserInfo() {
        if (_userInfo == null)
        {
            _userInfo = new userInfo();
        }
        return _userInfo;
    }
    
    public void showChatForm(Object chatObject, int type)
    {
        final String chatID;
        if (chatObject instanceof Account)
        {
            chatID = ((Account) chatObject).getUserID();
        }
        else
        {
            chatID = (String) chatObject;
        }
        
        if (!this.chatFormShown.containsKey(chatID))
        {
            JFrame chatForm = this.createChatForm(chatObject, type);
            chatForm.setResizable(false);
            chatForm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            chatForm.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    e.getWindow().dispose();
                    getChatFormShown().remove(chatID);
                }
            });
            
            this.getChatFormShown().put(chatID, chatForm);
        }
        
        JFrame chatForm = this.getChatFormShown().get(chatID);
        chatForm.setVisible(true);
    }
    
    public void hideChatForm(Object chatObject)
    {
        final String chatID;
        if (chatObject instanceof Account)
        {
            chatID = ((Account) chatObject).getUserID();
        }
        else
        {
            chatID = (String) chatObject;
        }
        
        JFrame chatForm = this.getChatFormShown().get(chatID);
        if (chatForm != null)
        {
            chatForm.dispose();
            this.getChatFormShown().remove(chatID);
        }
    }
        
    public JFrame createChatForm(Object chatObject, int type)
    {
        switch (type)
        {
            case CHATFORM_PRIVATE:
                return new privateChat((Account) chatObject);
            case CHATFORM_ROOM:
                return new roomChat((String) chatObject, (String) chatObject);
            case CHATFORM_WORLD:
                return new worldChat();
        }
        
        return null;
    }

    /**
     * @return the chatFormShown
     */
    public Map<String, JFrame> getChatFormShown() {
        return chatFormShown;
    }
    
    public void disposeAll()
    {
        if (this._friendRequest != null)
        {
            this._friendRequest.dispose();
        }
        if (this._loginForm != null)
        {
            this._loginForm.dispose();
        }
        if (this._menu != null)
        {
            this._menu.dispose();
        }
        if (this._userInfo != null)
        {
            this._userInfo.dispose();
        }
        
        for (String chatFormID : this.chatFormShown.keySet())
        {
            JFrame chatForm = this.chatFormShown.get(chatFormID);
            chatForm.dispose();
        }
    }
}
